package com.anche.cywebnew.Utils.HikUtil;

import com.anche.cywebnew.DTO.HikBaseDTO;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.text.MessageFormat;
import java.util.Calendar;

public class TimeUtil extends CommonUtil {

    private static final Logger logger = LoggerFactory.getLogger(CaptureUtil.class);

    public boolean doCheckTime(HikBaseDTO hikBaseDTO) {
        boolean isSuccess = false;
        if (doInit()) {
            NativeLong m_lUserId = doLogin(hikBaseDTO);
            if (m_lUserId.longValue() != -1) {
                Calendar now = Calendar.getInstance();
                HCNetSDK.NET_DVR_TIME strCurTime = new HCNetSDK.NET_DVR_TIME();
                strCurTime.dwYear = now.get(Calendar.YEAR);
                strCurTime.dwMonth = now.get(Calendar.MONTH) + 1;
                strCurTime.dwDay = now.get(Calendar.DAY_OF_MONTH);
                strCurTime.dwHour = now.get(Calendar.HOUR_OF_DAY);
                strCurTime.dwMinute = now.get(Calendar.MINUTE);
                strCurTime.dwSecond = now.get(Calendar.SECOND);
                strCurTime.write();
                Pointer lpPicConfig = strCurTime.getPointer();
                boolean setDVRConfigSuc = hCNetSDK.NET_DVR_SetDVRConfig(m_lUserId, HCNetSDK.NET_DVR_SET_TIMECFG,
                        new NativeLong(0), lpPicConfig, strCurTime.size());
                strCurTime.read();
                if (setDVRConfigSuc) {
                    logger.info("校时成功");
                    isSuccess = true;
                    doLogout(m_lUserId);
                } else {
                    int code = hCNetSDK.NET_DVR_GetLastError();
                    String msg = HikErrorUtil.getMsgByCode(code);
                    errMsg = MessageFormat.format(errMsg, "校时失败", code, msg);
                    logger.info("校时失败：错误码[{}]，错误描述[{}]", code, msg);
                }
            }
            doLogout(m_lUserId);
            doDestroy();
        }
        return isSuccess;
    }
}
